java为什么文件在使用另一个按钮后不会被删除
我在编辑。我还发现,在使用另一个按钮后无法删除它,因为它抛出了java。尼奥。文件文件系统异常:2018年6月30日星期六9时19分37秒。txt:该进程无法访问该文件,因为它正被另一个进程使用。第二个按钮的哪个代码部分导致系统继续读取或访问文件,因此第一个删除按钮无法删除文件? 我有一个从组合框中删除选定文件的按钮,还有一个显示当月利润的按钮。问题是,当我点击borrar(删除)按钮时,它会正确删除所选文件,但当我点击Ventas del mes(月利润)按钮,然后我想再次点击borrar(删除)按钮时,它不会从组合框中删除所选文件。这只会在每次单击“月利润”按钮后单击“删除”按钮时发生。我怎样才能解决这个问题
Set<String> results = new HashSet<String>();
Set<String> Año = new HashSet<String>();
Set<String> Mes = new HashSet<String>();
Set<String> Dia = new HashSet<String>();
Set<String> textos = new HashSet<String>();
String[] meses = {"enero", "febrero", "marzo", "abril", "mayo", "junio", "julio", "agosto", "septiembre", "octubre", "noviembre", "diciembre",};
Set<String> meses1 = new HashSet<String>();
File[] files = new File("C:\\Users\\SATELITE\\Documents\\NetBeansProjects\\Restaurante").listFiles();
public Recibos() {
initComponents();
for (File file : files) {
for (int i = 0; i < meses.length; i++) {
if (file.getName().contains(meses[i])) {
meses1.add(meses[i]);
}
}
if (file.isFile()) {
if (file.getName().contains(".txt")) {
results.add(file.getName());
}
}
}
DefaultComboBoxModel DefaultComboBoxModel2 = new DefaultComboBoxModel(meses1.toArray());
cbMes.setModel(DefaultComboBoxModel2);
}
然后我有第一个按钮
private void btnBorrarActionPerformed(java.awt.event.ActionEvent evt) {
txtMostrar.setText("");
results.clear();
System.out.println(results);
String text = cbRecibo.getSelectedItem().toString();
File[] files = new File("C:\\Users\\SATELITE\\Documents\\NetBeansProjects\\Restaurante").listFiles();
for (File file : files) {
if (file.isFile()) {
if (file.getName().equals(text)) {
Path p1 = Paths.get(text);
try {
java.nio.file.Files.deleteIfExists(p1);
} catch (IOException ex) {
Logger.getLogger(Recibos.class.getName()).log(Level.SEVERE, null, ex);
}
System.out.println(file + "xd1");
}
}
}
System.out.println(results+"results");
File[] files2 = new File("C:\\Users\\SATELITE\\Documents\\NetBeansProjects\\Restaurante").listFiles();
for (File file : files2) {
if (file.isFile()) {
if (file.getName().contains(".txt")) {
System.out.println(file + "xd2");
results.add(file.getName());
}
}
}
System.out.println(results);
cbRecibo.removeAll();
DefaultComboBoxModel DefaultComboBoxModel1 = new DefaultComboBoxModel(results.toArray());
cbRecibo.setModel(DefaultComboBoxModel1);
}
第二个按钮点击后,第一个按钮不会被删除
private void btnVentasMesActionPerformed(java.awt.event.ActionEvent evt) {
suma = 0;
List<String> list = new ArrayList<String>();
Iterator<String> iterator = results.iterator();
while (iterator.hasNext()) {
String setElement = iterator.next();
File file = new File(setElement);
BufferedReader reader = null;
try {
reader = new BufferedReader(new FileReader(file));
String text = null;
while ((text = reader.readLine()) != null) {
if (text.contains("Total")) {
list.add(text);
}
}
} catch (Exception e) {
}
}
//good way:
Iterator<String> iterator2 = list.iterator();
while (iterator2.hasNext()) {
String setElement = iterator2.next();
String numberOnly = setElement.replaceAll("[A-Z,a-z,:]", "");
suma = suma + Double.parseDouble(numberOnly);
}
String totalDeVentas = "La venta total del mes de " + cbMes.getSelectedItem().toString() + "\n fue de :" + suma;
txtMostrar.setText(totalDeVentas);
}
# 1 楼答案
我已经解决了。我没有关闭这个阅读器
谢谢你的帮助